package com.manager.user.module.controller;

import com.manager.commons.base.DataGrid;
import com.manager.core.web.json.AjaxJson;
import com.manager.user.module.entity.GenUser;
import com.manager.user.module.service.GenUserService;
import com.manager.user.web.controller.AdminController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * GenUserController
 * 用户表
 *
 * @author manager
 * @version 1.0
 */
@Controller()
public class GenUserController extends AdminController<GenUser> {
    private final String baseUrl = "/gen/user";
    @Autowired
    private GenUserService _service;

    /**
     * 删除数据
     *
     * @return
     */
    @RequestMapping(value = baseUrl + "/folder/delete", method = RequestMethod.POST)
    public @ResponseBody AjaxJson delete() {
        try {
            return _service.myDelete(getParams());
        } catch (Exception e) {
            return new AjaxJson("删除失败:" + e.getMessage());
        }
    }

    /**
     * @return
     */
    @RequestMapping(value = baseUrl + "/updateFileName", method = RequestMethod.POST)
    public @ResponseBody AjaxJson updateFileName() {
        try {
            return _service.updateName(getParams());
        } catch (Exception e) {
            return new AjaxJson("修改失败:" + e.getMessage());
        }
    }

    /**
     * @return
     */
    @RequestMapping(value = baseUrl + "/addFolder", method = RequestMethod.POST)
    public @ResponseBody AjaxJson addFolder() {
        try {
            return _service.addFolder(getParams());
        } catch (Exception e) {
            return new AjaxJson("添加失败:" + e.getMessage());
        }

    }

    /**
     * @return
     */
    @RequestMapping(value = baseUrl + "/upload", method = RequestMethod.POST)
    public @ResponseBody AjaxJson uploadRes(@RequestParam(value = "pid") String pid, @RequestParam(value = "file") MultipartFile file) {
        //定义允许上传的文件扩展名
        try {
            return _service.addFile(getUserId(), pid, file, getParams());
        } catch (Exception e) {
            return new AjaxJson("服务器异常，请稍后再试！");
        }
    }


    @RequestMapping(value = baseUrl + "/folderTree", method = RequestMethod.POST)
    public @ResponseBody AjaxJson folderTree() {
        return new AjaxJson(1, "请求成功", _service.folderTree(getParams()));
    }

    @RequestMapping(value = baseUrl + "/pid/folder", method = RequestMethod.POST)
    public @ResponseBody AjaxJson fileFolder() {
        return new AjaxJson(1, "请求成功", _service.fileFolder(getParams()));
    }

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(baseUrl + "/index")
    public ModelAndView index() {
        return getModelAndView("/gen/user-index");
    }

    /**
     * 新增
     *
     * @return
     */
    @GetMapping(baseUrl + "/add")
    public ModelAndView add() {
        return getModelAndView("/gen/user-edit", getModel());
    }

    /**
     * 新增
     *
     * @return
     */
    @RequestMapping(baseUrl + "/edit")
    public ModelAndView edit() {
        return getModelAndView("/gen/user-edit", _service.findMap(getParams()));
    }


    /**
     * 分页
     *
     * @return
     */
    @RequestMapping(value = baseUrl + "/list")
    public @ResponseBody DataGrid page() {
        return _service.queryPage(getParams());
    }

    /**
     * 分页
     *
     * @return
     */
    @RequestMapping(value = baseUrl + "/images")
    public @ResponseBody DataGrid image() {
        return _service.queryPageFiles(getParams());
    }

    @RequestMapping(value = baseUrl + "/search/images")
    public @ResponseBody DataGrid searchImage() {
        return _service.queryPageSearchFiles(getParams());
    }

    @Override
    public GenUser getModel() {
        return new GenUser();
    }

}
